package com.villaday.idf.activity;


import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter.PerformanceListAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.model.memberAchievements;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.DateUtils;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 业绩列表
 */
public class PerformanceListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, AbsRecyclerViewAdapter.OnItemClickListener, OnAdapterCallbackListener, OkHttpUtil.OnDataListener
        , AbsRecyclerViewAdapter.OnBtnListener {

    @BindView(R.id.toolbar_icon)
    Toolbar toolbar;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;

    @BindView(R.id.tv_sale_orderby)
    TextView tvSaleCondition;
    @BindView(R.id.tv_performance_orderby)
    TextView tvPerformacneCondition;


    Unbinder unbinder;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();

    private int pageNo = 1;
    private int nextIndex = 1;
    private int pageSize = 20;
    List<memberAchievements.BodyBean.ListBean> data = new ArrayList();
    private PerformanceListAdapter performanceListAdapter;

    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


    private int Salestatus = 1;
    private String startDate;
    private String endDate;


    @Override
    protected int getViewResId() {
        return R.layout.activity_performance_list;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbar, "业绩列表", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        srl.setOnRefreshListener(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        performanceListAdapter = new PerformanceListAdapter(this, this);
        rv.setAdapter(performanceListAdapter);

        rv.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 1), getResources().getColor(R.color.colorBg), PhoneUtil.dp2px(this, 8)));
        performanceListAdapter.setOnItemClickListener(this);
        performanceListAdapter.setOnBtnListener(this);
 /*       View inflate = View.inflate(this, R.layout.item_head_income_details, null);
        performanceListAdapter.addHeaderView(inflate);*/
        performanceListAdapter.setEmptyView(R.layout.empty_tips);
        performanceListAdapter.setEmptyTips(R.id.tv_emptyTips, "没有更多了");

//        startDate = dateFormat.format(date)+" 00:00:00";
//        endDate = dateFormat.format(date)+" 23:59:59";
        startDate = "";
        endDate = "";
    }

    @Override
    protected void loadData() {
        getData();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", pageNo + "");
        map.put("pageSize", pageSize + "");
        map.put("beginDateTime", startDate);
        map.put("endDateTime", endDate);
        map.put("orderType", Salestatus + "");
        LogeUtil.e("request", "Salestatus==" + Salestatus + " startDate==" + startDate + "  endDate==" + endDate);
        OkHttpUtil.getJSON(this, Constant.URL.member_achievements, map, this);
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        nextIndex = 1;
        getData();
    }

    //rv item 点击事件
    @Override
    public void onItemClick(View v, int position) {
/*        Intent intent = new Intent(this, FindDetailsActivity.class);
        intent.putExtra("topicId", data.get(position).getId());
        startActivity(intent);*/
    }

    @OnClick({R.id.tv_sale_orderby, R.id.tv_performance_orderby})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sale_orderby: {  //销量
                initSale();
            }
            break;
            case R.id.tv_performance_orderby: {  //业绩
                initPerformacne();
            }
            break;

        }

    }

    @Override
    public void onCallback() {
        if (nextIndex == pageNo + 1) {
            pageNo++;
            getData();
        }
    }

    @Override
    public void onResponse(String url, String json) {
        if (Constant.URL.member_achievements.equals(url)) {
            LogeUtil.e("member_achievements", json);
            dismissLoading();

            memberAchievements recommendEntity = gson.fromJson(json, memberAchievements.class);
            removeLoadingItem();
            if (pageNo == 1) {
                data.clear();
            }

            if ("200".equals(recommendEntity.getCode())) {
                   /* if (pageNo == 1 && presellEntity.getBody().getRecomendList() != null && presellEntity.getBody().getRecomendList().getList() != null) {

                        dataHead.addAll(presellEntity.getBody().getRecomendList().getList());
                        homeListSaleHeadAdapter.setData(dataHead);
                    }*/
                data.addAll(recommendEntity.getBody().getList());
                if (recommendEntity.getBody().isHasNext()) {
                    data.add(new memberAchievements.BodyBean.ListBean(1));
                    nextIndex = pageNo + 1;

                } else {
                    addBaseLine();
                }

            } else {
                addBaseLine();
            }
            performanceListAdapter.setData(data);
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {

        if (srl != null && srl.isRefreshing()) {
            srl.setRefreshing(false);
        }

    }

    private void removeLoadingItem() {
        if (data.size() > 0) {
            if (data.get(data.size() - 1).getMyType() == 1) {
                data.remove(data.size() - 1);
            }
        }
    }

    private void addBaseLine() {
        if (pageNo != 1) {
            data.add(new memberAchievements.BodyBean.ListBean(2));
        }
    }

    @Override
    public void function() {
        ToastUtil.initToast(this, "您想转发商品！");
    }


    private void initSale() {
        final ArrayList<String> list = new ArrayList<>();
        list.add("默认");
        list.add("按销量排序");
        list.add("按销售额排序");

        OptionsPickerView<String> mOptionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                Salestatus = options1;
                String sex = list.get(options1);
                tvSaleCondition.setText(sex);
                pageNo = 1;
                nextIndex = 1;
                loadData();
            }
        }).setCancelColor(getResources().getColor(R.color.colorRed))
                .setSubmitColor(getResources().getColor(R.color.colorRed)).build();
        // 设置数据
        mOptionsPickerView.setPicker(list);
        // 设置选项单位
        mOptionsPickerView.show();
    }


    private void initPerformacne() {
        final ArrayList<String> list = new ArrayList<>();
        list.add("今日业绩");
        list.add("本周业绩");
        list.add("本月业绩");
        list.add("总业绩");

        OptionsPickerView<String> mOptionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String sex = list.get(options1);
                tvPerformacneCondition.setText(sex);
                switch (sex) {
                    case "今日业绩":
                        startDate = dateFormat.format(date) + " 00:00:00";
                        endDate = dateFormat.format(date) + " 23:59:59";
                        break;
                    case "本周业绩":
                        startDate = DateUtils.getCurrentMonday() + " 00:00:00";
                        endDate = DateUtils.getPreviousSunday() + " 23:59:59";
                        break;
                    case "本月业绩":
                        startDate = DateUtils.getMinMonthDate(dateFormat.format(date)) + " 00:00:00";
                        endDate = DateUtils.getMaxMonthDate(dateFormat.format(date)) + " 23:59:59";
                        break;
                    case "总业绩":
                        startDate = "";
                        endDate = "";
                        break;
                }

                pageNo = 1;
                nextIndex = 1;
                loadData();
            }
        }).setCancelColor(getResources().getColor(R.color.colorRed))
                .setSubmitColor(getResources().getColor(R.color.colorRed)).build();
        // 设置数据
        mOptionsPickerView.setPicker(list);
        // 设置选项单位
        mOptionsPickerView.show();
    }
}
